/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import java.io.File;
import java.sql.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import utils.DataBaseReference;
import utils.ManejadorDB;

/**
 *
 * @author Jonathan
 */
public class BasicConfig extends DataBaseReference {

    
public static void populateVehicleColors() throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        String path = "src/resources/vehiculoColor.xml";
        String path2 = "C:/Documents and Settings/Jonathan/Mis documentos/workspace/test2/java/src/main/resources/xml/vehiculoColor.xml";

        Document doc = db.parse(new File(path));

        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_NAME);

        Statement stat = conn.createStatement();
        
        PreparedStatement prep = conn.prepareStatement(
                "insert into vehiculoColor values (?, ?);");


        for (int c = 0; doc.getElementsByTagName("Id").item(c) != null; c++) {
            String id = doc.getElementsByTagName("Id").item(c).getChildNodes().item(0).getNodeValue();
            String color = doc.getElementsByTagName("Nombre").item(c).getChildNodes().item(0).getNodeValue();

            prep.setString(1, id);
            prep.setString(2, color);
            prep.addBatch();
        }

        conn.setAutoCommit(false);
        prep.executeBatch();
        conn.setAutoCommit(true);

        /*
        ResultSet rs = stat.executeQuery("select * from vehiculoColor;");
        while (rs.next()) {
            System.out.println("id = " + rs.getString("id") + " color = " + rs.getString("color"));
        }
        rs.close();
        /**/
        conn.close();

    }
    
public static void populateClientes() throws Exception {
    /// TODO, JCE batch.
    ManejadorDB.ejecutarSQL("INSERT into individuo (primerNombre,segundoNombre,apellidoPaterno,apellidoMaterno,fechaNacimiento,genero) values('primerNombre','segundoNombre','apellidoPaterno','apellidoMaterno','12/29/1999','genero');");

}
        
public static void destruirTablas() throws Exception {
    ManejadorDB.ejecutarSQL("drop table if exists individuo;");
    ManejadorDB.ejecutarSQL("drop table if exists alerta;");
}

public static void reiniciarTablas() throws Exception {
    BasicConfig.destruirTablas();
    ManejadorDB.ejecutarSQL("CREATE TABLE individuo (id INTEGER PRIMARY KEY AUTOINCREMENT,primerNombre VARCHAR ,segundoNombre VARCHAR ,apellidoPaterno VARCHAR ,apellidoMaterno VARCHAR ,fechaCreacionInd DATE ,genero VARCHAR,cedula VARCHAR,direccion VARCHAR, telefono VARCHAR,movil VARCHAR, correo VARCHAR);");
    ManejadorDB.ejecutarSQL("CREATE TABLE alerta (id INTEGER PRIMARY KEY AUTOINCREMENT,cedula VARCHAR,fecha DATE ,fechaCreacion DATE,responsable VARCHAR ,centro VARCHAR ,descripcion TEXT);");
}
}
